package com.imooc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.imooc.pojo.Comments;
import com.imooc.pojo.Users;
import com.imooc.pojo.UsersReport;
import com.imooc.pojo.vo.PublisherVideo;
import com.imooc.pojo.vo.UsersVO;
import com.imooc.service.UserService;
import com.imooc.utils.IMoocJSONResult;
import com.imooc.utils.MD5Utils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.util.StringUtil;

/**
 * @author Chakid
 *
 */
@RestController
@Api(value="用户相关业务的接口",tags= {"用户相关业务的接口的controller"})
@RequestMapping("/user")
public class UserController extends BasicController{
	
	
	@Autowired
	private UserService userService;	
	
	
	@ApiOperation(value="用户上传头像",notes="用户上传头像的接口")
	@ApiImplicitParam(name="userId",value="用户Id",required=true,dataType="String",paramType="query")
	@PostMapping("/uploadFace")
	public IMoocJSONResult logout(String userId, @RequestParam("file") MultipartFile[] files) throws Exception {
		
		if(StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("用户的id不能为空，请重试...");
		}
		
		
		
		
		//文件保存命名空间
		String fileSpace="E:/imooc_videos_dev";
		//保存到数据库中的相对路径
		String uploadPathDB="/"+userId+"/face";
		
		FileOutputStream fileOutputStream=null;
		InputStream inputStream=null;
		
		try {
			if(files!=null&&files.length>0) {
				String fileName=files[0].getOriginalFilename();
				
				if(StringUtils.isNotBlank(fileName)) {
					//文件上传的最终保存路径
					String finalFacePath=fileSpace+uploadPathDB+"/"+fileName;
					//设置数据库中的保存路径
					uploadPathDB+=("/"+fileName);
					
					File outFile=new File(finalFacePath);
					
					//看是否存在用户存储对应的文件夹，如果不存在就创建
					if(outFile.getParentFile()!=null||!outFile.getParentFile().isDirectory()) {
						
						//创建父文件夹
						outFile.getParentFile().mkdirs();
					}
					
					fileOutputStream=new FileOutputStream(outFile);
					inputStream=files[0].getInputStream();
					IOUtils.copy(inputStream, fileOutputStream);
					
				}
				
			}else {
				return IMoocJSONResult.errorMsg("上传出错，请重试...");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return IMoocJSONResult.errorMsg("上传出错，请重试...");
		}finally {
			if(fileOutputStream!=null) {
				fileOutputStream.flush();
				fileOutputStream.close();
			}
		}
		
		Users user=new Users();
		user.setId(userId);
		user.setFaceImage(uploadPathDB);
		userService.updateUserInfo(user);
		
		return IMoocJSONResult.ok(uploadPathDB);
	}
	
	@ApiOperation(value="查询用户信息",notes="查询用户信息的接口")
	@ApiImplicitParam(name="userId",value="用户Id",required=true,dataType="String",paramType="query")
	@ApiImplicitParams({
		@ApiImplicitParam(name="userId",value="用户ID",required=true,dataType="String",paramType="query"),
		@ApiImplicitParam(name="fanId",value="粉丝ID",required=true,dataType="String",paramType="query")
	})
	@PostMapping("/query")
	public IMoocJSONResult query(String userId,String fanId) throws Exception {
		if(StringUtils.isBlank(userId)) {
			return IMoocJSONResult.errorMsg("用户的id不能为空，请重试...");
		}
		
		
		Users userInfo = userService.queryUserInfo(userId);
		UsersVO usersVO=new UsersVO();
		BeanUtils.copyProperties(userInfo, usersVO);
		
		
		usersVO.setFollow(userService.queryIsFollow(userId, fanId));
		
		return IMoocJSONResult.ok(usersVO);
		
	}
	
	

	@PostMapping("/queryPublisher")
	public IMoocJSONResult queryPublisher(String loginUserId,String videoId,String publisherUserId) throws Exception {
		if(StringUtils.isBlank(publisherUserId)) {
			return IMoocJSONResult.errorMsg("");
		}
		
		//1、查询视频发布者的信息
		Users userInfo = userService.queryUserInfo(publisherUserId);
		UsersVO publisher=new UsersVO();
		BeanUtils.copyProperties(userInfo, publisher);
		
		//2、查询当前登陆者和视频的点赞关系
		boolean userLikeVideo=userService.isUserLikeVideo(loginUserId, videoId);
		
		PublisherVideo bean =new PublisherVideo();
		
		bean.setPublisher(publisher);
		bean.setUserLikeVideo(userLikeVideo);
		
		
		return IMoocJSONResult.ok(bean);
		
	}
	
//关注
	@PostMapping("/beyourfans")
	public IMoocJSONResult beyourfans(String userId,String fanId) throws Exception {
		
		if(StringUtils.isBlank(userId)||StringUtils.isBlank(fanId)) {
			return IMoocJSONResult.errorMsg("");
		}
		
		
		userService.saveUserFanRelation(userId, fanId);
		
		return IMoocJSONResult.ok("关注成功...");
		
	}

	//取消关注
	@PostMapping("/dontbeyourfans")
	public IMoocJSONResult dontbeyourfans(String userId,String fanId) throws Exception {
		
		if(StringUtils.isBlank(userId)||StringUtils.isBlank(fanId)) {
			return IMoocJSONResult.errorMsg("");
		}
		
		
		userService.deleteUserFanRelation(userId, fanId);
		
		return IMoocJSONResult.ok("取消关注成功...");
		
	}
	
	
	//举报
	@PostMapping("/reportUser")
	public IMoocJSONResult reportUser(@RequestBody UsersReport usersReport) {
		
		//保存举报信息
		userService.reportUser(usersReport);
		
		return IMoocJSONResult.ok("举报成功...平台因你更买好");
		
	}
	
	
	
}













